package cn.edu.zjgsu.ito.controller;

import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import cn.edu.zjgsu.ito.service.AdminInfService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

@Before(AdminControllerInterceptor.class)
public class AdminInfController extends Controller {
	/*
	 * 张虎涛
	 * 2016/06/01
	 */
	private AdminInfService adminInfService =ServiceFactory.createAdminInfService();
	/**
	 * 前往密码修改页面
	 */

	public void toUpdateAPwd(){
		if(getSessionAttr("aNo")==null){
			redirect("/toErrors");	
		}else{
			render("correcttinfo.html");
		}
	}
	
	/**
	 * 管理员修改密码
	 */
	public void updateASpwd(){
		boolean tf1=false;
		boolean tf2=false;
		String message="";
		String oldPassword=getPara("oldPassword");
		String newPassword=getPara("newPassword");
		String rePassword=getPara("rePassword");
		
		if((oldPassword==null||oldPassword.equals("")) ){
			setAttr("message","原始密码不能为空");
			toUpdateAPwd();
			return;
		}
		if(newPassword==null||newPassword.equals("")||rePassword==null||rePassword.equals("")){
			System.out.println("message新密码不能为空");
			setAttr("message","新密码不能为空");
			toUpdateAPwd();
			return;
		}

		if(!newPassword.equals(rePassword)){
			setAttr("message","两次密码不一致");
			toUpdateAPwd();
			return;
		}
		try{
			String aNo=getSessionAttr("aNo");
			
			tf1=adminInfService.checkPassword(aNo, oldPassword);
			if(tf1){
				tf2=adminInfService.updatePassword(newPassword,aNo);
			}else{
				message="原密码错误";
				toUpdateAPwd();
				return;
			}
			if(tf2){
				System.out.println("4");
				redirect("/upPass");
				return;
			}else{
				message="密码修改失败";
			}
			setAttr("message",message);
			toUpdateAPwd();
			return;
			
		}catch(Exception e){
			e.printStackTrace();	
		}
		
	}

}
